/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */
package com.ykht.proj.activity.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ykht.proj.activity.entity.*;
import com.ykht.proj.activity.mapper.VGroupMapper;
import com.ykht.proj.activity.service.VDiscountPlanShopService;
import com.ykht.proj.activity.service.VGroupService;
import com.ykht.proj.activity.service.VGroupShopService;
import com.ykht.proj.activity.vo.VGroupShopVo;
import com.ykht.proj.activity.vo.VGroupVo;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 群组表
 *
 * @author csg
 * @date 2021-05-18 14:26:57
 */
@RequiredArgsConstructor
@Slf4j
@Service
public class VGroupServiceImpl extends ServiceImpl<VGroupMapper, VGroup> implements VGroupService {

	private final VGroupMapper vGroupMapper;
	/**
	 * 群组和门店关联表 Api  服务
	 */
	private final VGroupShopService vGroupShopService;

	/**
	 * 新增群组表
	 *
	 * @param vGroupVo
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void addVGroup(VGroupVo vGroupVo) {
		Long userId = SecurityUtils.getUserId();
		vGroupVo.setCreateUser(userId);
		vGroupVo.setCreateTime(LocalDateTime.now());
		VGroup vGroup = BeanUtil.toBean(vGroupVo, VGroup.class);
		this.save(vGroup);
		List<VGroupShop> planShopList = new ArrayList<>();
		vGroupVo.getSjObjectList().stream().forEach(itme -> {
			VGroupShop vDiscountPlanShop = new VGroupShop();
			vDiscountPlanShop.setGroupId(vGroup.getId());
			vDiscountPlanShop.setShopId(itme.getShopId());
			vDiscountPlanShop.setBrandId(itme.getBrandId());
			vDiscountPlanShop.setCompanyId(itme.getCompanyId());
			vDiscountPlanShop.setCreateUser(userId);
			vDiscountPlanShop.setCreateTime(LocalDateTime.now());
			planShopList.add(vDiscountPlanShop);
		});
		vGroupShopService.saveBatch(planShopList);
	}

	/**
	 * 修改群组表
	 *
	 * @param vGroupVo
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateVGroup(VGroupVo vGroupVo) {
		Long userId = SecurityUtils.getUserId();
		vGroupVo.setUpdateUser(userId);
		vGroupVo.setUpdateTime(LocalDateTime.now());
		VGroup vGroup = BeanUtil.toBean(vGroupVo, VGroup.class);
		this.updateById(vGroup);
		vGroupShopService.remove(Wrappers.lambdaQuery(VGroupShop.class)
				.eq(VGroupShop::getGroupId, vGroupVo.getId()));
		List<VGroupShop> planShopList = new ArrayList<>();
		vGroupVo.getSjObjectList().stream().forEach(itme -> {
			VGroupShop vDiscountPlanShop = new VGroupShop();
			vDiscountPlanShop.setGroupId(vGroup.getId());
			vDiscountPlanShop.setShopId(itme.getShopId());
			vDiscountPlanShop.setBrandId(itme.getBrandId());
			vDiscountPlanShop.setCompanyId(itme.getCompanyId());
			vDiscountPlanShop.setCreateUser(userId);
			vDiscountPlanShop.setCreateTime(LocalDateTime.now());
			planShopList.add(vDiscountPlanShop);
		});
		vGroupShopService.saveBatch(planShopList);
	}

	/**
	 * 门店群组分页查询
	 *
	 * @param page
	 * @param vGroup
	 * @return
	 */
	@Override
	public IPage<VGroupVo> getVGroupPage(Page page, VGroup vGroup) {
		IPage<VGroupVo> vGroupPage = vGroupMapper.getVGroupPage(page, vGroup);
		List<VGroupVo> records = vGroupPage.getRecords();
		if (null != records && records.size() > 0) {
			List<Long> longList = records.stream().map(VGroupVo::getId).collect(Collectors.toList());
			List<VGroupShopVo> shopVoList =	vGroupShopService.getCardCount(longList);
			if (null != shopVoList && shopVoList.size() >0){
				records.stream().forEach(itme -> {
					shopVoList.stream().forEach(groupId ->{
						if (itme.getId().equals(groupId.getGroupId())){
							itme.setShopCount(groupId.getShopCount());
						}
					});

				});
				vGroupPage.setRecords(records);
			}
		}
		return vGroupPage;
	}


	/**
	 * 刪除群组
	 *
	 * @param id
	 * @return
	 */
	@Override
	public R deleteGroup(Long id) {
		List<VCardGroup> cardGroupList = new VCardGroup().selectList(
				Wrappers.lambdaQuery(VCardGroup.class).eq(VCardGroup::getGroupId, id));
		if (null != cardGroupList && cardGroupList.size() > 0){
			return R.failed("该群组有参加的卡，请先删除卡！");
		}
		this.removeById(id);
		return R.ok(vGroupShopService.remove(Wrappers.lambdaQuery(VGroupShop.class).eq(VGroupShop::getGroupId, id)));
	}
}
